﻿$(function () {

    var footer = {

        init: function () {
            this.discoverChildren();
            this.initChildren();
            this.bindEvents();
            this.storeSelf();
        },

        discoverChildren: function () {
            this.$element = $("#footer");
            this.$pages = this.$element.find(".footer-page");
            this.$buttons = this.$element.find(".btn");
        },

        bindEvents: function () {
            EVENTBROKER.menuTopic.on(TOPIC_EVENTS.menuGoParent, this.onGoMenuParent.bind(this));
            EVENTBROKER.menuTopic.on(TOPIC_EVENTS.menuGo, this.onGoMenu.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchStarted, this.onMatchStarted.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchStarting, this.onMatchStarting.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchEnded, this.onMatchEnded.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchHintResult, this.onHintsResult.bind(this));
            EVENTBROKER.matchTopic.on(TOPIC_EVENTS.matchCompleted, this.onMatchCompleted.bind(this));
        },

        initChildren: function () {
            this.initMenus();
            this.initButtons();
        },

        initMenus: function () {
            this.$pages.each(function (i, e) {
                // WIDGETS.menu(e);
                if (e.id) {
                    if (WIDGETS[e.id])
                        WIDGETS[e.id](e);
                }
            });
        },

        initButtons: function () {
            this.$buttons.each(function (i, e) {
                WIDGETS.button(e);
            });
        },

        storeSelf: function () {
            this.$element.data("footer", this);
        },

        showPage: function (pageId) {
            this.$pages.removeClass("visible");
            this.$element.find("#" + pageId).addClass("visible");
        },

        onGoMenu: function (ev, menuId) {
            this.showPage(menuId);
        },

        onGoMenuParent: function (ev, menuId) {
            var parentId = APP.menuMap().getParentId(menuId);
            this.showPage(parentId);
        },

        onMatchStarted: function (ev, matchModel) {
            this.showPage("menu-play-keyboard");
        },

        onMatchStarting: function (ev) {
            this.showPage("footer-loader");
        },

        onMatchEnded: function (ev, matchModel) {
            this.showPage("menu-home");
        },

        onHintsResult: function (ev, result) {
            this.showPage("menu-hint-result");
        },

        onMatchCompleted: function (ev, result) {
            this.showPage("menu-play-completed");
        }
    }

    footer.init();

});
